package com.ly.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ly.mapper.TClassMapper;
import com.ly.mapper.TeachersMapper;
import com.ly.po.TClassPO;
import com.ly.pojo.Students;
import com.ly.pojo.TClass;
import com.ly.pojo.Users;
import com.ly.service.TClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class TClassServiceImpl implements TClassService {

    @Autowired
    private TClassMapper tClassMapper;

    @Autowired
    private TeachersMapper teachersMapper;

    @Override
    public PageInfo<TClass> getTClass(int page, int size) {
        PageHelper.startPage(page, size);
        List<TClass> tClass = tClassMapper.selectTClass();
        PageInfo<TClass> pageInfo = PageInfo.of(tClass);
        return pageInfo;
    }

    @Override
    public PageInfo<TClassPO> getTClassPO(int page, int size) {
        PageHelper.startPage(page, size);
        List<TClassPO> tClassPO = tClassMapper.selectTClassPO();
        PageInfo<TClassPO> pageInfo = PageInfo.of(tClassPO);
        return pageInfo;
    }

    @Override
    public PageInfo<TClass> getSearchTClass(int page, int size, String searchText) {
        PageHelper.startPage(page, size);
        List<TClass> tClass = tClassMapper.selectTClassBySearch(searchText);
        PageInfo<TClass> pageInfo = PageInfo.of(tClass);
        return pageInfo;
    }

    @Override
    public int deleteTClassById(Integer id) {
        int i2 = tClassMapper.deleteByPrimaryKey(id);
        if (i2 == 1) {
            return 1;
        }
        return 0;
    }

    @Override
    public int insertTClass(TClassPO tClassPO) {
        //先判断classCode是否唯一
        TClass s = tClassMapper.selectByCode(tClassPO.getClassCode());
        if (s != null){
            //说明重复了
            return 2;
        }

        TClass tClass = new TClass();
        tClass.setClassCode(tClassPO.getClassCode());
        tClass.setStudentCount(tClassPO.getStudentCount());
        tClass.setName(tClassPO.getName());
        tClass.setMajor(tClassPO.getMajor());
        tClass.setCollege(tClassPO.getCollege());
        tClass.setHeadTeacherTno(tClassPO.getHeadTeacherTno());
        tClass.setId(tClassPO.getId());

        //先拿到classCode写入users表
        int i = tClassMapper.insert(tClass);
        if (i == 1) {
            return 1;
        }
        return 0;
    }

    @Override
    public int updateTClass(TClassPO tClassPO) {
        //更新前先判断数据是否合理（在前端已经判断过是否合法）
        //班级代码唯一
        TClass s = tClassMapper.selectByCode(tClassPO.getClassCode());
        if (s != null && !s.getClassCode().equals(tClassPO.getClassCode())){
            //说明重复了
            return 2;
        }

        TClass tClass = new TClass();
        tClass.setClassCode(tClassPO.getClassCode());
        tClass.setStudentCount(tClassPO.getStudentCount());
        tClass.setName(tClassPO.getName());
        tClass.setMajor(tClassPO.getMajor());
        tClass.setCollege(tClassPO.getCollege());
        tClass.setHeadTeacherTno(tClassPO.getHeadTeacherTno());
        tClass.setId(tClassPO.getId());

        int i1 = tClassMapper.updateByPrimaryKey(tClass);
        if (i1 == 1) {
            return 1;
        }
        return 0;
    }

    @Override
    public int deleteHeadTeacherByTeacherId(Integer id) {
        Long tno = teachersMapper.selectTnoById(id);
        int i = tClassMapper.updateHeadTeacherByTno(tno);
        return i;
    }
}
